//
//  ZVolumeSlide.m
//  AppTestSlide
//
//  Created by ZStart on 13-8-16.
//  Copyright (c) 2013年 ZStart. All rights reserved.
//
#import "ConstansCode.h"
#import "FLVolSlide.h"
CGFloat width,height;

@implementation FLVolSlide



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initconfig];
    }
    return self;
}

-(id)init
{
    self=[super init];
    if (self) {
        [self initconfig];

    }
    return self;


}

-(id)initWithCoder:(NSCoder *)aDecoder
{

    self=[super initWithCoder:aDecoder];

    if (self) {
        
        [self initconfig];

    }
    return self;

}


-(void)initconfig
{
    self.backgroundColor = [UIColor whiteColor]; //背景颜色设置，设置为clearColor 背景透明
    
    
    width = self.bounds.size.width;
    height= self.bounds.size.height;
    
    UIView *view = [[UIView alloc]initWithFrame:self.bounds];
    view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"volumeCell1.png"]];
    [self addSubview:view];
    
    
    
    _processView = [[UIView alloc]init];
    _processView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"volumeCell2.png"]];
    _processView.frame = CGRectMake(0, 0, width * 0, height);
    
    [self addSubview:_processView];
    
    _slideView = [[UISlider alloc]initWithFrame:self.bounds];
    _slideView.value = 0;
    
    _slideView.maximumValue = 1.0;
    _slideView.minimumValue = 0.0;

    
    [_slideView addTarget:self action:@selector(slideValueChanged) forControlEvents:UIControlEventValueChanged];
    
    UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(slideTapValueChanged:)];

    
    [_slideView addGestureRecognizer:tap];
    
    
    
    [self addSubview:_slideView];

    [RACObserve(self, slideView) subscribeNext:^(UISlider *newName) {

        
        CGFloat value = _slideView.value;
        _processView.frame = CGRectMake(0, 0, width * value, height);
        [_slideView setMaximumTrackImage:[UIImage imageNamed:@"clearBack.png"] forState:UIControlStateNormal];
        [_slideView setMinimumTrackImage:[UIImage imageNamed:@"clearBack.png"] forState:UIControlStateNormal];

    
    }];
    
}

-(void)slideTapValueChanged:(UITapGestureRecognizer *)tap
{

    CGPoint p=[tap locationInView:tap.view];
    
    
    CGFloat value=p.x/width;

    [self setSlideValue:value];

    
    

}


- (void) setSlideValue:(CGFloat) value{
    _slideView.value = value;
    [self slideValueChanged];
}
- (void) slideValueChanged{
    CGFloat value = _slideView.value;
    _processView.frame = CGRectMake(0, 0, width * value, height);
    [_slideView setMaximumTrackImage:[UIImage imageNamed:@"clearBack.png"] forState:UIControlStateNormal];
    [_slideView setMinimumTrackImage:[UIImage imageNamed:@"clearBack.png"] forState:UIControlStateNormal];

    self.slideValueChageBlock(_slideView);
}
@end
